package com.z.redis.command.impl.set;

import com.z.redis.RedisCore;
import com.z.redis.command.Command;
import com.z.redis.command.CommandType;
import com.z.redis.datatype.BytesWrapper;
import com.z.redis.datatype.RedisSet;
import com.z.redis.resp.BulkString;
import com.z.redis.resp.Resp;
import com.z.redis.resp.RespInt;
import io.netty.channel.ChannelHandlerContext;

/**
 * scard key
 * 获得某个集合的元素总数
 */
public class Scard implements Command {
    private BytesWrapper key;

    @Override
    public CommandType type(){
        return CommandType.scard;
    }

    public void setContent(Resp[] array){
        key = ((BulkString) array[1]).getContent();
    }

    public void handle(ChannelHandlerContext ctx, RedisCore redisCore){
        RedisSet redisSet = (RedisSet) redisCore.get(key);
        int size = redisSet.scard();
        ctx.writeAndFlush(new RespInt(size));
    }
}
